package concurrency;//: concurrency/CallableDemo.java

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

class TaskWithResult implements Callable<Integer> {
  private int i;
  public TaskWithResult(int i) {
    this.i = i;
  }
  public Integer call() {
    return i;
  }
}

public class CallableDemo {
  public static void main(String[] args){
    List<Future<Integer>> result = new ArrayList<>();
    ExecutorService exec = Executors.newFixedThreadPool(8);
    for(int i =0; i < 10; i++) {
      result.add(exec.submit(new TaskWithResult(i)));
    }
//    for(int i = 0; i < 10; i++) {
//      result.add(new Thread(new TaskWithResult(i)).start());
//    }
    for(Future<Integer> fi: result) {
      try{
        System.out.println(fi.get());
      } catch (InterruptedException e) {
        System.out.println(e);
        return;
      } catch (ExecutionException e)  {
        System.out.println(e);
        return;
      } finally {
        exec.shutdown();
      }

    }
  }
} /* Output:
result of TaskWithResult 0
result of TaskWithResult 1
result of TaskWithResult 2
result of TaskWithResult 3
result of TaskWithResult 4
result of TaskWithResult 5
result of TaskWithResult 6
result of TaskWithResult 7
result of TaskWithResult 8
result of TaskWithResult 9
*///:~
